/*
 *  PreferencesWindow.c
 *  xGPS
 *
 *  Created by Miguel Arroz on 10/06/28.
 *  Copyright Miguel Arroz. All rights reserved.
 *
 */

#include "PreferencesWindow.h"

#define WINDOW_LEFT 200
#define WINDOW_TOP 530
#define WINDOW_RIGHT 500
#define WINDOW_BOTTOM 430

XPWidgetID windowID = 0;
XPWidgetID hostLabelID = 0;
XPWidgetID hostFieldID = 0;
//XPWidgetID portLabelID = 0;
//XPWidgetID portFieldID = 0;
XPWidgetID saveButtonID = 0;
XPWidgetID cancelButtonID = 0;
XPWidgetID helpLabelID = 0;

void destroyPreferencesWindow()
{
    if (windowID != 0)
    {
        xGPSDebug("Destroying preferences window");
        XPDestroyWidget(windowID, 1);
        windowID = 0;
    }
}

void showPreferencesWindow() {
	if( windowID == 0 ) {
		xGPSDebug( "Creating preferences window" );
		
		int x1=WINDOW_LEFT, y1=WINDOW_TOP, x2=WINDOW_RIGHT, y2=WINDOW_BOTTOM;
        windowID = XPCreateWidget(x1, y1, x2, y2, 0, "xGPS Preferences", 1, NULL, xpWidgetClass_MainWindow);
        XPSetWidgetProperty(windowID, xpProperty_MainWindowHasCloseBoxes, 1);
        XPAddWidgetCallback(windowID, windowCallback);
		
		x1 += 10; x2 -= 10;
        y1 -= 30; y2 += 10;
        
        hostLabelID = XPCreateWidget(x1, y1, x1 + 100, y1-20, 1, "iPhone IP:", 0, windowID, xpWidgetClass_Caption);
        hostFieldID = XPCreateWidget(x1 + 75, y1, x2, y1-20, 1, "1.1.1.1", 0, windowID, xpWidgetClass_TextField);
        XPAddWidgetCallback( hostFieldID, hostFieldCallback );
        y1 -= 20;
        //portLabelID = XPCreateWidget(x1, y1, x2, y1-20, 1, "Port:", 0, windowID, xpWidgetClass_Caption);
        //portFieldID = XPCreateWidget(x1 + 75, y1, x2, y1-20, 1, "12345", 0, windowID, xpWidgetClass_TextField);
		//XPAddWidgetCallback( portFieldID, portFieldCallback );
        //y1 -= 22;
        
		helpLabelID = XPCreateWidget(x1 + 35, y1 + 3, x2, y1-17, 1, "(use the IP that shows up on iPhone xGPS app)", 0, windowID, xpWidgetClass_Caption);
        y1 -= 22;
		
		cancelButtonID = XPCreateWidget(x2-170, y1, x2-90, y1-20, 1, "Cancel", 0, windowID, xpWidgetClass_Button);
        XPSetWidgetProperty(cancelButtonID, xpProperty_ButtonType, xpButtonBehaviorPushButton);
        XPSetWidgetProperty(cancelButtonID, xpProperty_ButtonBehavior, xpButtonBehaviorPushButton);
        XPAddWidgetCallback(cancelButtonID, cancelButtonCallback);
        saveButtonID = XPCreateWidget(x2-80, y1, x2, y1-20, 1, "OK", 0, windowID, xpWidgetClass_Button);
        XPSetWidgetProperty(saveButtonID, xpProperty_ButtonType, xpButtonBehaviorPushButton);
        XPSetWidgetProperty(saveButtonID, xpProperty_ButtonBehavior, xpButtonBehaviorPushButton);
        XPAddWidgetCallback(saveButtonID, saveButtonCallback);
	}
	
	updateWindowWithValues();
	
	XPShowWidget( windowID );
}

void updateWindowWithValues( void ) {
	//char port[10];
	//sprintf(port, "%hu", getXGPSPort() );
	XPSetWidgetDescriptor( hostFieldID, (char*) getXGPSHost() );
	//XPSetWidgetDescriptor( portFieldID, port );
}

void saveValuesFromWindow( void ) {
	char host[256];
	XPGetWidgetDescriptor( hostFieldID, host, 256 );
	host[255] = '\0';
	setXGPSHost(host);

	//char port[256];
	//XPGetWidgetDescriptor( portFieldID, port, 256 );
	//port[255] = '\0';
	
	//setXGPSPort( (unsigned short) strtol(port, (char **)NULL, 10) );
	
	savePreferences();
}

/*int portFieldCallback(XPWidgetMessage inMessage, XPWidgetID inWidget, long inParam1, long inParam2) {
	if (inMessage == xpMsg_KeyPress)
    {
        // Only allow numeric
        XPKeyState_t *key_state = (XPKeyState_t *)inParam1;
        char key = key_state->key;
        char vkey = key_state->vkey;
        if (key == '\n' || key == '\r')
        {
            XPLoseKeyboardFocus(inWidget);
            return 1;
        }
        if ((key_state->flags & (xplm_ControlFlag | xplm_ShiftFlag | xplm_OptionAltFlag)) ||
            ((key < '0' || key > '9') &&
             (key != 0x7f) && (key != '\b') &&
             (vkey != XPLM_VK_LEFT) && (vkey != XPLM_VK_RIGHT) &&
             (vkey != XPLM_VK_ESCAPE)))
        {
            return 1;
        }
    }
	
	return 0;
} */

int hostFieldCallback(XPWidgetMessage inMessage, XPWidgetID inWidget, long inParam1, long inParam2) {
	if (inMessage == xpMsg_KeyPress)
    {
        // Only allow numeric and '.'
        XPKeyState_t *key_state = (XPKeyState_t *)inParam1;
        char key = key_state->key;
        char vkey = key_state->vkey;
        if (key == '\n' || key == '\r')
        {
            XPLoseKeyboardFocus(inWidget);
            return 1;
        }
        if ((key_state->flags & (xplm_ControlFlag | xplm_ShiftFlag | xplm_OptionAltFlag)) ||
            ((key < '0' || key > '9') && key != '.' &&
             (key != 0x7f) && (key != '\b') &&
             (vkey != XPLM_VK_LEFT) && (vkey != XPLM_VK_RIGHT)))
        {
            return 1;
        }
    }
	
	return 0;
}

int cancelButtonCallback(XPWidgetMessage inMessage, XPWidgetID inWidget, long inParam1, long inParam2)
{
    if (inMessage == xpMsg_PushButtonPressed)
    {
        XPHideWidget(windowID);
        return 1;
    }
    return 0;
}


int saveButtonCallback(XPWidgetMessage inMessage, XPWidgetID inWidget, long inParam1, long inParam2)
{
    if (inMessage == xpMsg_PushButtonPressed)
    {
        saveValuesFromWindow();
		XPHideWidget(windowID);
	    return 1;
    }
    return 0;
}


int windowCallback(XPWidgetMessage inMessage, XPWidgetID inWidget, long inParam1, long inParam2) {
	if (inMessage == xpMessage_CloseButtonPushed)
	{
        destroyPreferencesWindow();
        return 1;
    }
	
	return 0;
}
